package com.nmsl.system.util;

import lombok.Data;

@Data
public class R {
    private int code;
    private String msg;
    private Object data;

    // 返回一个状态码为200，提示信息为“操作成功”的R对象
    public static R ok() {
        return new R(200, "操作成功", null);
    }

     //返回一个操作成功的R对象，data为传入的数据
    public static R ok(Object data) {
        return new R(200, "操作成功", data);
    }

    // 返回一个状态码为200，消息为msg，数据为null的R对象
    public static R ok(String msg) {
        return new R(200, msg, null);
    }


    public static R ok(Boolean flag) {
        return new R(flag);
    }

    ;

    public static R fail(int code, String msg) {
        // 返回一个R对象，code为传入的code，msg为传入的msg，data为null
        return new R(code, msg, null);
    }

    public static R fail(String msg) {
        return new R(300, msg, null);
    }

    public static R fail() {
        return new R(300, "操作失败", null);
    }

    public static R error() {
        return new R(500, "error", null);
    }

    public static R error(Object e) {
        return new R(500, "error", e);
    }

    public static R error(String msg) {
        return new R(500, msg, null);
    }

    public static R error(int code, String msg) {
        return new R(code, msg, null);
    }

    public R() {
        this.code = 0;
        this.msg = "";
        this.data = null;
    }

    public R(Object data) {
        this.code = 200;
        this.msg = "success";
        this.data = data;
    }

    public R(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public R(Boolean flag) {
        if (flag) {
            this.code = 200;
        } else {
            this.code = 600;
            this.msg = "缺少token";
        }

    }

}
